home *** CD-ROM | disk | FTP | other *** search
/ MacWorld 2005 March / Macworld CD March 2005 - Marathon Trilogy.iso / Shareware World / iPod / iPodderX.sit / iPodderX / iPodderX.app / Contents / Resources / btformats.py < prev    next >
Encoding:
Python Source  |  2005-01-07  |  3.7 KB  |  101 lines

  1. # Written by Bram Cohen
  2. # see LICENSE.txt for license information
  3.  
  4. from types import StringType, LongType, IntType, ListType, DictType
  5. from re import compile
  6.  
  7. reg = compile(r'^[^/\\.~][^/\\]*$')
  8.  
  9. ints = (LongType, IntType)
  10.  
  11. def check_info(info):
  12.     if type(info) != DictType:
  13.         raise ValueError, 'bad metainfo - not a dictionary'
  14.     pieces = info.get('pieces')
  15.     if type(pieces) != StringType or len(pieces) % 20 != 0:
  16.         raise ValueError, 'bad metainfo - bad pieces key'
  17.     piecelength = info.get('piece length')
  18.     if type(piecelength) not in ints or piecelength <= 0:
  19.         raise ValueError, 'bad metainfo - illegal piece length'
  20.     name = info.get('name')
  21.     if type(name) != StringType:
  22.         raise ValueError, 'bad metainfo - bad name'
  23.     if not reg.match(name):
  24.         raise ValueError, 'name %s disallowed for security reasons' % name
  25.     if info.has_key('files') == info.has_key('length'):
  26.         raise ValueError, 'single/multiple file mix'
  27.     if info.has_key('length'):
  28.         length = info.get('length')
  29.         if type(length) not in ints or length < 0:
  30.             raise ValueError, 'bad metainfo - bad length'
  31.     else:
  32.         files = info.get('files')
  33.         if type(files) != ListType:
  34.             raise ValueError
  35.         for f in files:
  36.             if type(f) != DictType:
  37.                 raise ValueError, 'bad metainfo - bad file value'
  38.             length = f.get('length')
  39.             if type(length) not in ints or length < 0:
  40.                 raise ValueError, 'bad metainfo - bad length'
  41.             path = f.get('path')
  42.             if type(path) != ListType or path == []:
  43.                 raise ValueError, 'bad metainfo - bad path'
  44.             for p in path:
  45.                 if type(p) != StringType:
  46.                     raise ValueError, 'bad metainfo - bad path dir'
  47.                 if not reg.match(p):
  48.                     raise ValueError, 'path %s disallowed for security reasons' % p
  49.         for i in xrange(len(files)):
  50.             for j in xrange(i):
  51.                 if files[i]['path'] == files[j]['path']:
  52.                     raise ValueError, 'bad metainfo - duplicate path'
  53.  
  54. def check_message(message):
  55.     if type(message) != DictType:
  56.         raise ValueError
  57.     check_info(message.get('info'))
  58.     if type(message.get('announce')) != StringType:
  59.         raise ValueError
  60.  
  61. def check_peers(message):
  62.     if type(message) != DictType:
  63.         raise ValueError
  64.     if message.has_key('failure reason'):
  65.         if type(message['failure reason']) != StringType:
  66.             raise ValueError
  67.         return
  68.     peers = message.get('peers')
  69.     if type(peers) == ListType:
  70.         for p in peers:
  71.             if type(p) != DictType:
  72.                 raise ValueError
  73.             if type(p.get('ip')) != StringType:
  74.                 raise ValueError
  75.             port = p.get('port')
  76.             if type(port) not in ints or p <= 0:
  77.                 raise ValueError
  78.             if p.has_key('peer id'):
  79.                 id = p.get('peer id')
  80.                 if type(id) != StringType or len(id) != 20:
  81.                     raise ValueError
  82.     elif type(peers) != StringType or len(peers) % 6 != 0:
  83.         raise ValueError
  84.     interval = message.get('interval', 1)
  85.     if type(interval) not in ints or interval <= 0:
  86.         raise ValueError
  87.     minint = message.get('min interval', 1)
  88.     if type(minint) not in ints or minint <= 0:
  89.         raise ValueError
  90.     if type(message.get('tracker id', '')) != StringType:
  91.         raise ValueError
  92.     npeers = message.get('num peers', 0)
  93.     if type(npeers) not in ints or npeers < 0:
  94.         raise ValueError
  95.     dpeers = message.get('done peers', 0)
  96.     if type(dpeers) not in ints or dpeers < 0:
  97.         raise ValueError
  98.     last = message.get('last', 0)
  99.     if type(last) not in ints or last < 0:
  100.         raise ValueError
  101.